package com.level.system.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class EmrParserUtil {
    /**
     * post请求emr数据
     *
     * @param url
     * @param paramsMap
     * @return
     * @throws Exception
     */
    public static JSONObject emrParser(String url, Map<String, Object> paramsMap) {
        JSONObject jsonResult = new JSONObject();
        Map<String, Object> postMap = new HashMap<>();
        postMap.put("data", paramsMap);
        String result = null;

        System.out.println("emrParser请求：" + url);
        System.out.println("emrParser请求参数：" + postMap);

        try {
            result = HttpClientUtil.getInstance().doPost(url, postMap);
            System.out.println("emrParser请求结果：");
            System.out.println(result);

            jsonResult = JSON.parseObject(result);

        } catch (Exception e) {
            System.out.println("emrParser 请求错误！" + url);
            e.printStackTrace();
        }


        return jsonResult;
    }


    /**
     * 按字节长度截取字符串
     *
     * @param str
     * @param subSLength
     * @return
     */
    public static String subStr(String str, int subSLength) {
        String subStr = "";
        try {
            if (str == null) return "";
            else {
                int tempSubLength = subSLength;//截取字节数
                subStr = str.substring(0, str.length() < subSLength ? str.length() : subSLength);//截取的子串
                int subStrByetsL = subStr.getBytes("UTF-8").length;//截取子串的字节长度
                // 说明截取的字符串中包含有汉字
                while (subStrByetsL > tempSubLength) {
                    int subSLengthTemp = --subSLength;
                    subStr = str.substring(0, subSLengthTemp > str.length() ? str.length() : subSLengthTemp);
                    subStrByetsL = subStr.getBytes("UTF-8").length;
                }
            }
        } catch (Exception e) {
            System.out.println("EmrParserUtil.subStr错误：" + e.getMessage());
        }
        return subStr;
    }

}
